import { Separator } from "@/components/ui/separator"
import { type SearchParams } from "@/types/table"
import * as React from "react"
import { getValidFilters } from "@/lib/data-table"
import { searchParamsCache } from "@/lib/admin-users/validations"
import { getAllRoles, getUsersEVCP } from "@/lib/users/service"
import { getUserCountGroupByRole } from "@/lib/admin-users/service"
import { DataTableSkeleton } from "@/components/data-table/data-table-skeleton"
import { UserTable } from "@/lib/users/table/users-table"

interface IndexPageProps {
  searchParams: Promise<SearchParams>
}

export default async function SystemUserPage(props: IndexPageProps) {

  const searchParams = await props.searchParams
  const search = searchParamsCache.parse(searchParams)

  const validFilters = getValidFilters(search.filters)

  const promises = Promise.all([
    getUsersEVCP({
      ...search,
      filters: validFilters,
    }),
    getUserCountGroupByRole(),
    getAllRoles()
  ])

  return (
    <React.Suspense
      fallback={
        <DataTableSkeleton
          columnCount={6}
          searchableColumnCount={1}
          filterableColumnCount={2}
          cellWidths={["10rem", "12rem", "12rem", "12rem"]}
          shrinkZero
        />
      }
    >
      <div className="space-y-6">
        <div>
          <h3 className="text-lg font-medium">Users</h3>
          <p className="text-sm text-muted-foreground">
            시스템 전체 사용자들을 조회하고 관리할 수 있는 페이지입니다. 사용자에게 롤을 할당하는 것으로 메뉴별 권한을 관리할 수 있습니다.
          </p>
        </div>
        <Separator />
        <UserTable promises={promises} />
      </div>
    </React.Suspense>

  )
}